package com.kelong.front.service.impl;

import com.kelong.front.dao.UserDao;
import com.kelong.front.pojo.User;
import com.kelong.front.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kelong.front.vo.SystemLoginVo;
import java.util.List;
import java.util.Optional;
import com.kelong.front.validate.Test;

/**
 * Title: UserServiceImpl
 * Description:
 * 用户操作实现类
 * Version:1.0.0
 *
 * @author pancm
 * @date 2018年3月19日
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    @Override
    public SystemLoginVo  addUser(Test loginsValidate) {

        String username = loginsValidate.getUsername();
        System.out.println(username);
//        boolean flag = false;
        try {
            User model = new User();
            long id = 3;
            model.setId(id);
            model.setName("787878uj");
            model.setPassword("ooooo");
            model.setAge(8);
            userDao.save(model);

        } catch (Exception e) {
            System.out.println("新增失败!");
            e.printStackTrace();
        }
//        return flag; name avatar roleName
//        Object  vo=new Object() ;
//
//        System.out.println(vo);
        SystemLoginVo vo = new SystemLoginVo();
        int  x=100;
        vo.setId(x);
        vo.setToken("tttt8888");
        vo.setName("yhhh");
        vo.setAvatar("9999");
        vo.setRoleName("9999");

        return vo;
    }

    @Override
    public boolean updateUser(User user) {
        boolean flag = false;
        try {
            userDao.save(user);
            flag = true;
        } catch (Exception e) {
            System.out.println("修改失败!");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteUserById(User user) {
        boolean flag = false;
        try {
            long id= user.getId();
            userDao.deleteById(id);
            flag = true;
        } catch (Exception e) {
            System.out.println("删除失败!");
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean deleteUser( List<Long> ids ) {
        boolean flag = false;
        try {
            userDao.deleteAllById(ids);
            flag = true;
        } catch (Exception e) {
            System.out.println("删除失败!");
            e.printStackTrace();
        }
        return flag;
    }


    @Override
    public Optional<User> findUserById(User user) {
        long id= user.getId();
        return userDao.findById(id);
    }

    @Override
    public List<User> findUsersByIds( List<Long> ids) {

        return userDao.findAllById(ids);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }


    @Override
    public List<User> findByAge(int age) {
        return userDao.findUsersByAgeGreaterThan(age);
    }

}
